#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cmsis_os2.h"
#include "ohos_init.h"

#include "iot_uart.h"
#include "FS704U.h"
#include "utils.h"

#define FS704U_RX_TASK_STACK_SIZE 1024 * 25
#define FS704U_RX_TASK_PRIO 23
#define FS704U_UART_BUFF_SIZE 16
#define FS704U_UART_STR_SIZE 400
#define FS704U_UART_Data_SIZE 400
#define FS704U_FLAGS_MSK1 0x00000001U
#define FS704U_DTU_UART 2

osThreadAttr_t FS704U_attr;
osEventFlagsId_t FS704U_evtID;

uint8_t FS704U_uartBuff[FS704U_UART_BUFF_SIZE];
uint8_t * FS704U_uartBuffPtr = FS704U_uartBuff;
uint8_t FS704U_uartStr[FS704U_UART_STR_SIZE];
uint8_t * FS704U_uartStrPtr = FS704U_uartStr;
uint8_t FS704U_uartData[FS704U_UART_Data_SIZE];
uint8_t * FS704U_uartDataPtr = FS704U_uartData;

// char FS704U_hello[3] = {0xC3,0xC3,0xC3};
// char * FS704U_helloPtr = FS704U_hello;

char * FS704U_sockind = "admin,sockind,off\r\n";    // 关闭socket区分
char * FS704U_save = "admin,save\r\n";              // 保存配置
char * FS704U_sleep = "admin, powermode,0\r\n";     // 取消休眠模式

static int FS704U_uartLength = 0;
static int FS704U_uartEndDataLength = 0;

static void *FS704U_Config_Task(const char *arg){
    (void)arg;

    FS704U_uartLength = 0;

    while (1) {
        int length = IoTUartRead(FS704U_DTU_UART, FS704U_uartBuffPtr, FS704U_UART_BUFF_SIZE);

        // printf("[DM4GDTU]uart2 length:%d\r\n",length);
        // mm_print_hex(FS704U_uartBuff,length);

        memcpy((char *)FS704U_uartStrPtr+FS704U_uartLength,(char *)FS704U_uartBuffPtr,length);
        FS704U_uartLength = FS704U_uartLength + length;

        if(length < FS704U_UART_BUFF_SIZE || FS704U_uartBuff[15] == 0x0A){        // 会话结束
            memcpy((char *)FS704U_uartDataPtr,(char *)FS704U_uartStrPtr,FS704U_uartLength);
            FS704U_uartEndDataLength = FS704U_uartLength;
            FS704U_uartLength = 0;
            FS704U_sendOSEvent();
            memset(FS704U_uartStr,0,FS704U_UART_STR_SIZE);
        }

        memset(FS704U_uartBuff,0,FS704U_UART_BUFF_SIZE);
    }

    return NULL;
}

boolean FS704U_SendHello(void){
    memset(FS704U_uartData,0,FS704U_UART_Data_SIZE);
    IoTUartWrite(FS704U_DTU_UART, FS704U_sockind, strlen(FS704U_sockind));
    osDelay(10);
    IoTUartWrite(FS704U_DTU_UART, FS704U_sleep, strlen(FS704U_sleep));
    osDelay(10);
    IoTUartWrite(FS704U_DTU_UART, FS704U_save, strlen(FS704U_save));  
    osDelay(10);
    FS704U_uartEndDataLength = 0;
    FS704U_uartLength = 0;
    memset(FS704U_uartData,0,FS704U_UART_Data_SIZE);
    memset(FS704U_uartStr,0,FS704U_UART_STR_SIZE);
    memset(FS704U_uartBuff,0,FS704U_UART_BUFF_SIZE);
    return FALSE;
}

char* FS704U_SendMsg(char * sendData,int ** getDataLength,int sendDataLength){
    memset(FS704U_uartData,0,FS704U_UART_Data_SIZE);
    osEventFlagsClear(FS704U_evtID,FS704U_FLAGS_MSK1);
    IoTUartWrite(FS704U_DTU_UART, sendData, sendDataLength);
    osEventFlagsWait(FS704U_evtID, FS704U_FLAGS_MSK1, osFlagsWaitAny, 2000);
    *getDataLength = FS704U_uartEndDataLength + 0;
    FS704U_uartEndDataLength = 0;
    return (char*)FS704U_uartData;
}

void FS704U_sendOSEvent(void){
    osEventFlagsSet(FS704U_evtID, FS704U_FLAGS_MSK1);
    osThreadYield();
}

void FS704U_ConfigThread(void){
    FS704U_attr.name = "FS704U_Config_Task";
    FS704U_attr.attr_bits = 0U;
    FS704U_attr.cb_mem = NULL;
    FS704U_attr.cb_size = 0U;
    FS704U_attr.stack_mem = NULL;
    FS704U_attr.stack_size = FS704U_RX_TASK_STACK_SIZE;
    FS704U_attr.priority = FS704U_RX_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)FS704U_Config_Task, NULL, &FS704U_attr) == NULL) {
        printf("[FS704UConfigExample] Falied to create FS704UTask!\n");
    }

    FS704U_evtID = osEventFlagsNew(NULL);
}